package com.yc.Testjvm.hotload;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;

/**
 * 加载扩展新类
 * @param args
 */
public class ExtNewClassLoad extends ClassLoader {
	
	//放扩展class的目录
	public static String ExtClassPath = "F:/git-my/jdemo/target/classes/";
	
	@Override
	protected Class<?> findClass(String name) throws ClassNotFoundException {
		try {
			File f = new File(ExtClassPath + name.replace(".", "/") + ".class");
			if (f.exists()) {
				InputStream is = new FileInputStream(f);
				byte[] bytes = IOUtils.toByteArray(is);
				is.close();
	            return defineClass(name, bytes, 0, bytes.length);
			}
		} catch (Exception e) {
			throw new ClassNotFoundException();
		}
		
		return super.findClass(name);
	}
	
}
